﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.CRM.BigCustomerDriver;

namespace BCData.CRM.BigCustomerDriver
{
    public class BigCustomerDriverData : IBigCustomerDriverData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.BigCustomerDriverId,
		    A.CompanyId,
		    A.CustomerInfoId,
		    A.BigCustomerId,
		    A.CustomerUserName,
		    A.DriverId,
		    A.DriverPhone,
		    A.DriverName,
		    A.VehicleTypeId,
		    A.VehicleType,
		    A.VehicleCategoryId,
		    A.CategoryName,
		    A.Remark,
		    A.CreateTime,
		    A.CreateAdminId,
		    A.CreateAdminName,
		    A.UpdateTime,
		    A.EditAdminId,
		    A.EditAdminName
        FROM
            CRM_BigCustomerDriver A";



        #region 构造函数
        public BigCustomerDriverData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        /// <summary>
        /// 添加新大客户专属司机纪录
        /// </summary>
        /// <param name="bigCustomerDriverEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerDriverEntity> InsertAsync(BigCustomerDriverEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        /// <summary>
        /// 修改大客户专属司机记录
        /// </summary>
        /// <param name="bigCustomerDriverEntity"></param>
        /// <returns></returns>
        public async Task<BigCustomerDriverEntity> UpdateAsync(BigCustomerDriverEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        /// <summary>
        /// 删除大客户专属司机记录
        /// </summary>
        /// <param name="bigCustomerDriverId"></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(long bigCustomerDriverId)
        {
            string sql = "DELETE FROM crm_bigcustomerdriver WHERE BigCustomerDriverId = ?bigCustomerDriverId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerDriverId", bigCustomerDriverId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        /// <summary>
        /// 获取大客户专属司机记录
        /// </summary>
        /// <param name="bigCustomerDriverId"></param>
        /// <returns></returns>
        public async Task<BigCustomerDriverEntity> GetEntityAsync(long bigCustomerDriverId)
        {
            string sql = QUERY + " WHERE  A.BigCustomerDriverId = ?bigCustomerDriverId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerDriverId", bigCustomerDriverId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<BigCustomerDriverEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询大客户专属司机记录
        public async Task<(IEnumerable<BigCustomerDriverEntity>, int)> GetListAsync(int? companyId,long? bigCustomerId, string driverName, string driverPhone,int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (companyId.HasValue)
            {
                wheres.Add("CompanyId=?CompanyId");
                parameters.Add("?CompanyId", companyId, DbType.Int64);
            }

            if(bigCustomerId.HasValue)
            {
                wheres.Add("BigCustomerId=?BigCustomerId");
                parameters.Add("?BigCustomerId", bigCustomerId, DbType.Int64);
            }

            if (!string.IsNullOrWhiteSpace(driverName))
            {
                wheres.Add("DriverName LIKE ?DriverName");
                parameters.Add("?DriverName", $"%{driverName.Trim()}%", DbType.AnsiString);
            }
            if (!string.IsNullOrWhiteSpace(driverPhone))
            {
                wheres.Add("DriverPhone LIKE ?DriverPhone");
                parameters.Add("?DriverPhone", $"%{driverPhone.Trim()}%", DbType.AnsiString);
            }


            string sql = string.Format(@"{0} {1} ; SELECT COUNT(1) FROM  crm_bigcustomerdriver  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize
                  );

            return await this.database.Slave.QueryPageableListAsync<BigCustomerDriverEntity>(sql, parameters);
        }
        #endregion

        #region GetOnlyList
        public async Task<IEnumerable<BigCustomerDriverEntity>> GetOnlyList(int? companyId, long? bigCustomerId, long? driverId)
        {
            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (companyId.HasValue)
            {
                wheres.Add("CompanyId=?CompanyId");
                parameters.Add("?CompanyId", companyId, DbType.Int64);
            }
            if (bigCustomerId.HasValue)
            {
                wheres.Add("BigCustomerId=?BigCustomerId");
                parameters.Add("?BigCustomerId", bigCustomerId, DbType.Int64);
            }
            if (driverId.HasValue)
            {
                wheres.Add("DriverId=?DriverId");
                parameters.Add("?DriverId", driverId, DbType.Int64);
            }

            string sqlWhere = wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "";

            return await this.database.Slave.QueryAsync<BigCustomerDriverEntity>(QUERY + sqlWhere, parameters);
        }
        #endregion
    }
}
//接口



